home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
AOL File Library: 2,801 to 2,900
/
aol-file-protocol-4400-2801-to-2900.zip
/
AOLDLs
/
C++ Files Library
/
Acere (Card Game)
/
AcereÄ.sit
/
Acereƒ
/
CTextDoc.cp
< prev
next >
Wrap
Text File
|
1994-08-25
|
8KB
|
318 lines
// ===========================================================================
// CTextDoc.cp ⌐1994 Metrowerks Inc. All rights reserved.
// ===========================================================================
//
// A simple text document class. It can handle opening, saving, and
// reverting TEXT files. It displays the text in a Window and can
// print the text.
#include "AcereApp.h"
#include "CTextDoc.h"
#include "WellStack.h"
#include "WellFreeCell.h"
#include "WellDeck.h"
#include "CardDeck.h"
#include <LWindow.h>
#include <LFile.h>
#include <LPrintout.h>
#include <LPlaceHolder.h>
#include <UMemoryMgr.h>
#include <UWindows.h>
#include <String_Utils.h>
#include <LTextEdit.h>
#include <Sound.h>
CTextDoc *theDoc;
const ResIDT WIND_TextDoc = 500;
const ResIDT prto_TextDoc = 201;
#define kVictorySound 1000
// const OSType Creator_DemoDoc = 'PPd0';
const ResIDT STRx_Untitled = 300;
// ---------------------------------------------------------------------------
// Ñ CTextDoc(LCommander*, FSSpec*)
// ---------------------------------------------------------------------------
// Construct a TextDoc associated with the specified file
//
// If inFileSpec is nil, then create an empty, untitled document
CTextDoc::CTextDoc(
LCommander *inSuper,
FSSpec *inFileSpec)
: LSingleDoc(inSuper)
{
// CGrafPtr theMacPort;
// PixPatHandle thePixPat;
// short i;
theDeck = new CardDeck;
currentDeckWell = currentFreeCell = currentStack = 0;
cardRgn = NewRgn();
theDoc = this;
firstCard.card = secondCard.card = kNoCard;
// Create window for our document
mWindow = LWindow::CreateWindow(WIND_TextDoc, this);
// Specify that the text view should
// be the Target when the Window
// is activated
// mTextView = (CDirtyText*) mWindow->FindPaneByID('Text');
// mWindow->SetLatentSub(mTextView);
if (inFileSpec == nil) {
NameNewDoc(); // Set name of untitled window
} else {
OpenFile(*inFileSpec); // Display contents of file in window
}
// theMacPort = (CGrafPtr)mWindow->GetMacPort();
// SetPort((GrafPtr)theMacPort);
// thePixPat = GetPixPat(6009);
// HLock((Handle)thePixPat);
// ((CGrafPtr)mWindow)->bkPixPat = GetPixPat(6009);
// SetPortPix((*thePixPat)->patMap);
}
CTextDoc::~CTextDoc() // destructor
{
delete theDeck;
theDeck = nil;
theDoc = nil;
}
// ---------------------------------------------------------------------------
// Ñ NameNewDoc
// ---------------------------------------------------------------------------
// Name a new, untitled document window
//
// Untitled windows start with "untitled", then "untitled 1",
// "untitled 2", etc. Old numbers are reused, so there won't be
// gaps in the numbering.
//
// This routine uses a STR# resource to store the "untitled" string,
// which can be localized to different languages. The first string
// is "untitled" and the second is "untitled " (trailing space),
// which is used when appending a number to the name.
void
CTextDoc::NameNewDoc()
{
// Start with the default name ("untitled")
Str255 name;
::GetIndString(name, STRx_Untitled, 1);
long num = 0;
while (UWindows::FindNamedWindow(name) != nil)
{
// An existing window has the current name
// Increment counter and try again
::GetIndString(name, STRx_Untitled, 2);
num++;
Str15 numStr;
::NumToString(num, numStr);
ConcatPStr(name, numStr);
}
mWindow->SetDescriptor(name); // Finally, set window title
// PlaceCards();
}
// ---------------------------------------------------------------------------
// Ñ OpenFile
// ---------------------------------------------------------------------------
// Open a new document for the specified File
void
CTextDoc::OpenFile(
FSSpec &inFileSpec)
{
// Create a new File object, read the entire File contents,
// put the contents into the text view, and set the Window
// title to the name of the File.
Try_ {
mFile = new LFile(inFileSpec);
mFile->OpenDataFork(fsRdWrPerm);
Handle textH = mFile->ReadDataFork();
mTextView->SetTextHandle(textH);
::DisposeHandle(textH);
mWindow->SetDescriptor(inFileSpec.name);
mIsSpecified = true;
}
Catch_(inErr) {
delete this;
Throw_(inErr);
} EndCatch_
}
// ---------------------------------------------------------------------------
// Ñ IsModified
// ---------------------------------------------------------------------------
// Return whether the Document is has changed since the last save
Boolean
CTextDoc::IsModified()
{
// Document has changed if the text view is dirty
// mIsModified = mTextView->IsDirty();
return mIsModified;
}
// ---------------------------------------------------------------------------
// Ñ DoAESave
// ---------------------------------------------------------------------------
// Save Document in the specified file with the specified file type
//
// If file type is fileType_Default, use the normal file type for
// this document
void
CTextDoc::DoAESave(
FSSpec &inFileSpec,
OSType inFileType)
{
delete mFile; // Kill existing file
mFile = new LFile(inFileSpec); // Make new file object
OSType fileType = GameFileType; // Find proper file type
if (inFileType != fileType_Default)
{
fileType = inFileType;
}
// Make new file on disk
mFile->CreateNewDataFile(OurCreator, GameFileType, 0);
mFile->OpenDataFork(fsRdWrPerm);
DoSave(); // Write out data
// Change window name
mWindow->SetDescriptor(inFileSpec.name);
}
// ---------------------------------------------------------------------------
// Ñ DoSave
// ---------------------------------------------------------------------------
// Save the entire Document to its associated File (which must already exist)
void
CTextDoc::DoSave()
{
// Get text and write to file
Handle textH = mTextView->GetTextHandle();
StHandleLocker theLock(textH);
mFile->WriteDataFork(*textH, GetHandleSize(textH));
// mTextView->SetDirty(false); // Saving makes doc un-dirty
}
// ---------------------------------------------------------------------------
// Ñ DoRevert
// ---------------------------------------------------------------------------
// Revert the Document to the last saved version on disk
void
CTextDoc::DoRevert()
{
Handle textH = mFile->ReadDataFork();
mTextView->SetTextHandle(textH);
::DisposeHandle(textH);
mTextView->Refresh();
}
// ---------------------------------------------------------------------------
// Ñ DoPrint
// ---------------------------------------------------------------------------
// Print the contents of the Document
void
CTextDoc::DoPrint()
{
LPrintout *thePrintout = LPrintout::CreatePrintout(prto_TextDoc);
LPlaceHolder *textPlace = (LPlaceHolder*)
thePrintout->FindPaneByID('TBox');
textPlace->InstallOccupant(mTextView, atNone);
thePrintout->DoPrintJob();
delete thePrintout;
}
void CTextDoc::CheckVictory(void)
{
short i;
Handle mySndHandle; // handle to an 'snd ' resource
SndChannelPtr mySndChan; // pointer to a sound channel
OSErr myErr;
for (i= 0; i < 4; i++)
{
if (theDeckWells[i]->itsCard.card != 13) // it's not a king
return;
}
mySndChan = nil; // Initialize channel pointer for error checking
// Read in 'snd' resource from resource file
mySndHandle = GetResource ('snd ', kVictorySound);
// check for a nil handle
if ( mySndHandle != nil )
{
myErr = SndPlay (mySndChan, mySndHandle, true);
}
}
void CTextDoc::StartNewGame(void)
{
short i;
theDeck->ZapOldDeck();
theDeck->GenerateNewDeck();
theDoc->currentDeckWell = theDoc->currentFreeCell = theDoc->currentStack = 0;
for (i= 0; i < 4; i++)
{
theDeckWells[i]->itsCard.card = kNoCard;
theDeckWells[i]->Draw(nil);
theFreeCells[i]->itsCard.card = kNoCard;
theFreeCells[i]->Draw(nil);
}
for (i=0; i < 8; i++)
{
theStacks[i]->InitCards();
}
}